package com.yoomiito.app.taobao;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.util.List;

public class AppUtils {

    /**
     * 判断是否安装了淘宝
     */
    public static boolean isTaoBaoAvailable(Context context) {
        return isPackageAvailable(context, "com.taobao.taobao");
    }


    public static boolean isPackageAvailable(Context context, String pkgName) {
        List<PackageInfo> pinfo = getCachedList(context);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName.toLowerCase();
                if (pn.equals(pkgName)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static List<PackageInfo> getCachedList(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> cachedList = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        return cachedList;
    }

}
